package com.mz.ozy.activity.me;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.mz.ozy.R;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.AboutBean;
import com.mz.ozy.bean.NetBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.ModifyPwdPresenter;
import com.mz.ozy.mvp.view.ModifyPwdContract;
import com.mz.ozy.utils.MD5Util;
import com.mz.ozy.utils.SPUtils;
import com.mz.ozy.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PayPwdActivity extends BaseActivity<ModifyPwdContract.View, ModifyPwdPresenter> implements ModifyPwdContract.View {

    @BindView(R.id.tv_phone)
    TextView mTvPhone;
    @BindView(R.id.et_code)
    EditText mEtCode;
    @BindView(R.id.bt_code)
    TextView mBtCode;
    @BindView(R.id.et_pwd)
    EditText mEtPwd;
    @BindView(R.id.et_pwd2)
    EditText mEtPwd2;
    @BindView(R.id.bt_confirm)
    Button mBtConfirm;
    private boolean canClick = true;//是否可以点击
    private MyCount mc;
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    canClick = true;
                    mBtCode.setText(R.string.get_code);
//                    mBtCode.setBackgroundResource(R.drawable.bt_round_bule_2);

                    break;
                case 2:
                    mBtCode.setText(msg.obj + "s");
                    break;
                default:
                    break;

            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_pwd);
        ButterKnife.bind(this);
        initTitle();
        tvTitle.setVisibility(View.GONE);
        tvCenterTitle.setText(R.string.paypwd);
        mc = new MyCount(60 * 1000, 1000);
        String username = SPUtils.getInstance(this).getString(Constants.SPName.USERNAME, "");
        if (!TextUtils.isEmpty(username)) {
            final String phonestr = username.trim().substring(0, 3) + "****" + username.trim().substring(username.trim().length() - 4);
            mTvPhone.setText(getString(R.string.str_ver_phone, phonestr));
        }
    }

    /*定义一个倒计时的内部类*/
    class MyCount extends CountDownTimer {
        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            handler.sendEmptyMessage(1);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            Message msg = new Message();
            msg.what = 2;
            msg.obj = millisUntilFinished / 1000;
            handler.sendMessage(msg);

        }
    }


    @OnClick({R.id.bt_code, R.id.bt_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_code:
                if (!canClick) {
                    return;
                }
                mPresenter.getCode(this, SPUtils.getInstance(this).getString(Constants.SPName.USER, "").trim());
                break;
            case R.id.bt_confirm:

                if (TextUtils.isEmpty(mEtCode.getText().toString().trim())) {
                    ToastUtils.showToast(getString(R.string.str_sms_code));
                    return;
                }
                if (TextUtils.isEmpty(mEtPwd.getText().toString().trim()) || mEtPwd.getText().toString().trim().length() != 6) {
                    ToastUtils.showToast(getString(R.string.str_paypwd_format_hint));
                    return;
                }

                if (TextUtils.isEmpty(mEtPwd2.getText().toString().trim())) {
                    ToastUtils.showToast(getString(R.string.str_again_input_login_pwd_hint));
                    return;
                }
                if (!mEtPwd.getText().toString().trim().equals(mEtPwd2.getText().toString().trim())) {
                    ToastUtils.showToast(getString(R.string.str_loginpwd_inconsistent));
                    return;
                }
                mPresenter.obtainModifyPwd(this, "1", mEtCode.getText().toString().trim(), MD5Util.getMD5String(mEtPwd.getText().toString().trim()));
                break;
        }
    }

    @Override
    public void obtainModifyPwdSuccess(Object o) {
        ToastUtils.showToast(getString(R.string.str_modify_paypwd_success));
        SPUtils.getInstance(this).putInt("paynum", 1);
        finish();

    }

    @Override
    public void getCodeSuccess(Object o) {
        canClick = false;
        ToastUtils.showToast(getString(R.string.str_send_sms_success));
        mc.start();

    }

    @Override
    public void getSuccess(AboutBean o) {

    }

    @Override
    public void handleFailResponse(NetBean bean) {
        super.handleFailResponse(bean);
        canClick = true;

    }
}
